package org.web3j.protocol.core.methods.response;

import java.io.IOException;
import java.util.Optional;

import org.web3j.protocol.ObjectMapperFactory;
import org.web3j.protocol.core.Response;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectReader;

public class EthDebugTraceTransaction extends Response<TraceTransaction> {

	public Optional<TraceTransaction> getTraceTransaction() {
		return Optional.ofNullable(getResult());
	}
	
	public static class ResponseDeserialiser extends JsonDeserializer<TraceTransaction> {

		private ObjectReader objectReader = ObjectMapperFactory.getObjectReader();
		
		@Override
		public TraceTransaction deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
			if (jsonParser.getCurrentToken() != JsonToken.VALUE_NULL) {
				return objectReader.readValue(jsonParser, TraceTransaction.class);
			} else {
				return null; // null is wrapped by Optional in above getter
			}
		}
	}
}
